﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using Business.IManager;
using Business.Manager;
using Entity;


namespace NetWorkPayment.BcakWard
{
    public partial class PostUtilityPyament : System.Web.UI.Page
    {
        //有两个问题
        //gridviw中的dropDownList动态绑定
        //gridview中的dropdownList字段取得值
        IAccommodationManager accommodationManager = new AccommodationManager();
        IUtilityPaymentManager utilityPaymentManager = new UtilityPaymentManager();
        String[] dormitoryAddress = { "南区", "A区", "B区", "C区", "D区" };//A，B，C，D，N
        String[] dormitoryNumforN = { "1-1", "1-2", "2-1","2-2", "3-1", "3-2", "4-1", "4-2", "5-1", "5-2", "5-3" };
        String[] dormitoryNumforA = { "A1-1", "A1-2","A2-1", "A2-2", "A3-1", "A3-2", "A4-1", "A4-2", "A5-1", "A5-2", "A6-1","A6-2","A7-1","A7-2","A8-1","A8-2"};
        String[] dormitoryNumforB = { "B1-1", "B1-2", "B2-1", "B2-2", "B3-1", "B3-2", "B4-1", "B4-2", "B5-1", "B5-2", "B6-1", "B6-2", "B7-1", "B7-2", "B8-1", "B8-2" };
        String[] dormitoryNumforC = { "C1-1", "C1-2", "C2-1", "C2-2", "C3-1", "C3-2", "C4-1", " C4-2", "C5-1", "C5-2", "C6-1", "C6-2", "C7-1", "C7-2", "C8-1", "C8-2" };
        String[] dormitoryNumforD = { "D1-1", "D1-2", "D2-1", "D2-2", "D3-1", "D3-2", "D4-1", " D4-2", "D5-1", "D5-2", "D6-1", "D6-2", "D7-1", "D7-2", "D8-1", "D8-2" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               lblDate.Text = System.DateTime.Now.ToShortDateString();
                BindData();
                DataTable table = new DataTable();
                table.Columns.Add(new DataColumn("ID"));
                table.Columns.Add(new DataColumn("dorNumList"));
                table.Columns.Add(new DataColumn("waterUseNum"));
                table.Columns.Add(new DataColumn("elecUseNum"));
                DataRow row = table.NewRow(); 
                table.Rows.Add(row);
                GridView1.DataSource = table; GridView1.DataBind();
 
            }
        }

        private void BindData()
        {
            dormitoryLocationList.DataSource = dormitoryAddress;
            dormitoryLocationList.DataBind();
            dormitoryBuildNumList.DataSource = dormitoryNumforN;
            dormitoryBuildNumList.DataBind();
            DataTable dt = accommodationManager.getDormitoryNum("南区","南区1-1");
            ViewState["tbl"] = dt;
            dt.Dispose();
        }

        protected void dormitoryLocationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            String str = dormitoryLocationList.SelectedItem.Text;
            if (str.Equals("南区"))
            {
                dormitoryBuildNumList.DataSource = dormitoryNumforN;
                dormitoryBuildNumList.DataBind();
            }
            else if (str.Equals("A区"))
            {
                dormitoryBuildNumList.DataSource = dormitoryNumforA;
                dormitoryBuildNumList.DataBind();
                DataTable dt =accommodationManager.getDormitoryNum(dormitoryLocationList.SelectedItem.Text, dormitoryLocationList.SelectedItem.Text+"A1-1");
                ViewState["tbl"] = dt;
                dt.Dispose();
                
            }
            else if (str.Equals("B区"))
            {
                dormitoryBuildNumList.DataSource = dormitoryNumforB;
                dormitoryBuildNumList.DataBind();
                DataTable dt = accommodationManager.getDormitoryNum(dormitoryLocationList.SelectedItem.Text, dormitoryLocationList.SelectedItem.Text+"B1-1");
                ViewState["tbl"] = dt;
                dt.Dispose();
            }
            else if(str.Equals("C区"))
            {
                dormitoryBuildNumList.DataSource = dormitoryNumforC;
                dormitoryBuildNumList.DataBind();
                DataTable dt = accommodationManager.getDormitoryNum(dormitoryLocationList.SelectedItem.Text, dormitoryLocationList.SelectedItem.Text+"C1-1");
                ViewState["tbl"] = dt;
                dt.Dispose();
            }
            else
            {
                dormitoryBuildNumList.DataSource = dormitoryNumforD;
                dormitoryBuildNumList.DataBind();
                DataTable dt = accommodationManager.getDormitoryNum(dormitoryLocationList.SelectedItem.Text, dormitoryLocationList.SelectedItem.Text+"D1-1");
                ViewState["tbl"] = dt;
                dt.Dispose();
            }
          
        }

        protected void dormitoryBuildNumList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = accommodationManager.getDormitoryNum(dormitoryLocationList.SelectedItem.Text,dormitoryBuildNumList.SelectedItem.Text);
            ViewState["tbl"] = dt;
            dt.Dispose();
        }
        #region
         protected void lbtnAddRow_Click(object sender, EventArgs e) 
       { 
         DataTable table = GetGridViewData(); DataRow newRow = table.NewRow();
         newRow["ID"] = Guid.NewGuid().ToString(); table.Rows.Add(newRow);
         GridView1.DataSource = table; GridView1.DataBind(); 
        }    
        private DataTable GetGridViewData() { 
            DataTable table = new DataTable(); 
            table.Columns.Add(new DataColumn("ID"));
            table.Columns.Add(new DataColumn("dorNumList"));
            table.Columns.Add(new DataColumn("waterUseNum")); 
            table.Columns.Add(new DataColumn("elecUseNum")); 
            foreach (GridViewRow row in GridView1.Rows) { 
                DataRow sourseRow = table.NewRow(); sourseRow["ID"] = row.Cells[0].Text;
                sourseRow["dorNumList"]=((DropDownList)row.FindControl("dorNumList")).SelectedValue;
                sourseRow["waterUseNum"] = ((TextBox)row.Cells[4].FindControl("txtWaterUseNum")).Text;
                sourseRow["elecUseNum"] = ((TextBox)row.Cells[5].FindControl("txtElecUseNum")).Text; 
                table.Rows.Add(sourseRow);
            } 
            return table; 
        }    
        protected void btnDeleteRow_Click(object sender, EventArgs e) 
        { 
            DataTable table = GetGridViewData(); 
            foreach (GridViewRow row in GridView1.Rows) { 
                if (((HtmlInputCheckBox)row.Cells[2].FindControl("chkRow")).Checked) { 
                   foreach (DataRow dtRow in table.Rows) { 
                       if (dtRow["ID"].ToString() == row.Cells[0].Text) { 
                           table.Rows.Remove(dtRow); break; 
                       } 
                   } 
                } 
            } 
            GridView1.DataSource = table; GridView1.DataBind(); 
        }
        #endregion


        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            List<UtilityPaymentProject> payments = new List<UtilityPaymentProject>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                UtilityPaymentProject payment = new UtilityPaymentProject();
                payment.WaterMeterNum = decimal.Parse(((TextBox)row.Cells[4].FindControl("txtWaterUseNum")).Text);
                payment.ElecMeterNum = decimal.Parse(((TextBox)row.Cells[5].FindControl("txtElecUseNum")).Text);
                payment.Dormitory = new Dormitory();
                payment.Dormitory.DormitoryId = ((DropDownList)row.FindControl("dorNumList")).SelectedValue;//Convert.ToInt32(((DropDownList)row.FindControl("dorNumList")).SelectedValue);
                int index = Convert.ToInt32(payment.Dormitory.DormitoryId);
                if (System.DateTime.Today.Month < 10)
                {
                    payment.Date = System.DateTime.Today.Year + "0" + System.DateTime.Today.Month;
                }
                else
                {
                    payment.Date = System.DateTime.Today.Year + "" + System.DateTime.Today.Month;
                }
                payments.Add(payment);
            }
            if (utilityPaymentManager.geneUtilityPayment(payments))
            {
                Response.Redirect("/test.aspx");
            }
        }
    }
    }

